package com.chinasofti.java8.demo04;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class TestStream {
    //1.创建stream
    @Test
    public  void test1(){
        //1.Collection 提供了两个方法
        List<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();//顺序流
        // Stream<String> parallelStream = list.parallelStream();//并行流

        //2。Arrays中stream（）
        Integer [] nums = new Integer[10];
        Stream<Integer> stream1 = Arrays.stream(nums);

        //3.Stream.of
        Stream<Integer> stream2 = Stream.of(1,2,3,4,5,6);

        //4.创建无限流
        Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 2).limit(10);

        Stream<Double> stream4 = Stream.generate(Math::random);
    }
    List<Employee> emps = Arrays.asList(
              new Employee(101, "张三", 18, 9999.99)
            , new Employee(102, "李四", 59, 6666.66)
            , new Employee(103, "王五", 28, 3333.33)
            , new Employee(104, "赵六", 8, 7777.7)
            , new Employee(105, "田七", 38, 5555.55)
            , new Employee(105, "田七", 38, 5555.55)
    );
    /*\
    筛选与切片
    filter 接收lambda，从流中排除某些元素
    limit 截断流，使其元素不超过指定数量
    skip（n）跳过元素，返回一个扔掉了前n个元素的流，若流中不足n个，则返回一个空流
    disinct --筛选 调用hashcode和equals方法重写
    */
    @Test
    public void test2() {//内部迭代
        Stream<Employee> stream = emps.stream().filter((e) -> {
            System.out.println("中间操作");
            return e.getAge() <= 35;
        });
    }
        @Test
        public void test3 () {//内部迭代
           emps.stream().filter((e) -> {
                System.out.println("....");
                return e.getSalary() >=5000;
            }).limit(2).forEach(System.out :: println);
        }
    @Test
    public void test4 () {//内部迭代
        emps.stream().filter((e) -> {
            System.out.println("....");
            return e.getSalary() >=5000;
        }).skip(2).forEach(System.out :: println);
    }
    @Test
    public void test5 () {//内部迭代
        emps.stream().distinct().forEach(System.out :: println);
    }

}
